home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / src / strftime.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  7KB  |  283 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *      This product includes software developed by the University of
  16.  *      California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)strftime.c    5.11 (Berkeley) 2/24/91";
  36. #endif                /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/types.h>
  39. #include <sys/time.h>
  40. #include <tzfile.h>
  41. #include <string.h>
  42.  
  43. static char *afmt[] =
  44. {
  45.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  46. };
  47. static char *Afmt[] =
  48. {
  49.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  50.     "Saturday",
  51. };
  52. static char *bfmt[] =
  53. {
  54.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  55.     "Oct", "Nov", "Dec",
  56. };
  57. static char *Bfmt[] =
  58. {
  59.     "January", "February", "March", "April", "May", "June", "July",
  60.     "August", "September", "October", "November", "December",
  61. };
  62.  
  63. static size_t gsize;
  64. static char *pt;
  65. static int _add(register char *), _conv(int, int, char), _secs(struct tm *);
  66.  
  67. static size_t _fmt(register char *, struct tm *);
  68.  
  69. size_t
  70. strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
  71. {
  72.     pt = s;
  73.     if ((gsize = maxsize) < 1)
  74.     return (0);
  75.     if (_fmt(format, t)) {
  76.     *pt = '\0';
  77.     return (maxsize - gsize);
  78.     }
  79.     return (0);
  80. }
  81.  
  82. static size_t
  83.  _fmt(register char *format, struct tm *t)
  84. {
  85.     for (; *format; ++format) {
  86.     if (*format == '%')
  87.         switch (*++format) {
  88.         case '\0':
  89.             --format;
  90.             break;
  91.         case 'A':
  92.             if (t->tm_wday < 0 || t->tm_wday > 6)
  93.             return (0);
  94.             if (!_add(Afmt[t->tm_wday]))
  95.             return (0);
  96.             continue;
  97.         case 'a':
  98.             if (t->tm_wday < 0 || t->tm_wday > 6)
  99.             return (0);
  100.             if (!_add(afmt[t->tm_wday]))
  101.             return (0);
  102.             continue;
  103.         case 'B':
  104.             if (t->tm_mon < 0 || t->tm_mon > 11)
  105.             return (0);
  106.             if (!_add(Bfmt[t->tm_mon]))
  107.             return (0);
  108.             continue;
  109.         case 'b':
  110.         case 'h':
  111.             if (t->tm_mon < 0 || t->tm_mon > 11)
  112.             return (0);
  113.             if (!_add(bfmt[t->tm_mon]))
  114.             return (0);
  115.             continue;
  116.         case 'C':
  117.             if (!_fmt("%a %b %e %H:%M:%S %Y", t))
  118.             return (0);
  119.             continue;
  120.         case 'c':
  121.             if (!_fmt("%m/%d/%y %H:%M:%S", t))
  122.             return (0);
  123.             continue;
  124.         case 'D':
  125.             if (!_fmt("%m/%d/%y", t))
  126.             return (0);
  127.             continue;
  128.         case 'd':
  129.             if (!_conv(t->tm_mday, 2, '0'))
  130.             return (0);
  131.             continue;
  132.         case 'e':
  133.             if (!_conv(t->tm_mday, 2, ' '))
  134.             return (0);
  135.             continue;
  136.         case 'H':
  137.             if (!_conv(t->tm_hour, 2, '0'))
  138.             return (0);
  139.             continue;
  140.         case 'I':
  141.             if (!_conv(t->tm_hour % 12 ?
  142.                    t->tm_hour % 12 : 12, 2, '0'))
  143.             return (0);
  144.             continue;
  145.         case 'j':
  146.             if (!_conv(t->tm_yday + 1, 3, '0'))
  147.             return (0);
  148.             continue;
  149.         case 'k':
  150.             if (!_conv(t->tm_hour, 2, ' '))
  151.             return (0);
  152.             continue;
  153.         case 'l':
  154.             if (!_conv(t->tm_hour % 12 ?
  155.                    t->tm_hour % 12 : 12, 2, ' '))
  156.             return (0);
  157.             continue;
  158.         case 'M':
  159.             if (!_conv(t->tm_min, 2, '0'))
  160.             return (0);
  161.             continue;
  162.         case 'm':
  163.             if (!_conv(t->tm_mon + 1, 2, '0'))
  164.             return (0);
  165.             continue;
  166.         case 'n':
  167.             if (!_add("\n"))
  168.             return (0);
  169.             continue;
  170.         case 'p':
  171.             if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
  172.             return (0);
  173.             continue;
  174.         case 'R':
  175.             if (!_fmt("%H:%M", t))
  176.             return (0);
  177.             continue;
  178.         case 'r':
  179.             if (!_fmt("%I:%M:%S %p", t))
  180.             return (0);
  181.             continue;
  182.         case 'S':
  183.             if (!_conv(t->tm_sec, 2, '0'))
  184.             return (0);
  185.             continue;
  186.         case 's':
  187.             if (!_secs(t))
  188.             return (0);
  189.             continue;
  190.         case 'T':
  191.         case 'X':
  192.             if (!_fmt("%H:%M:%S", t))
  193.             return (0);
  194.             continue;
  195.         case 't':
  196.             if (!_add("\t"))
  197.             return (0);
  198.             continue;
  199.         case 'U':
  200.             if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
  201.                    2, '0'))
  202.             return (0);
  203.             continue;
  204.         case 'W':
  205.             if (!_conv((t->tm_yday + 7 -
  206.                 (t->tm_wday ? (t->tm_wday - 1) : 6))
  207.                    / 7, 2, '0'))
  208.             return (0);
  209.             continue;
  210.         case 'w':
  211.             if (!_conv(t->tm_wday, 1, '0'))
  212.             return (0);
  213.             continue;
  214.         case 'x':
  215.             if (!_fmt("%m/%d/%y", t))
  216.             return (0);
  217.             continue;
  218.         case 'y':
  219.             if (!_conv((t->tm_year + TM_YEAR_BASE)
  220.                    % 100, 2, '0'))
  221.             return (0);
  222.             continue;
  223.         case 'Y':
  224.             if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
  225.             return (0);
  226.             continue;
  227.         case 'Z':
  228.             if (!t->tm_zone || !_add(t->tm_zone))
  229.             return (0);
  230.             continue;
  231.         case '%':
  232.             /*
  233.              * X311J/88-090 (4.12.3.5): if conversion char is
  234.              * undefined, behavior is undefined.  Print out the
  235.              * character itself as printf(3) does.
  236.              */
  237.         default:
  238.             break;
  239.         }
  240.     if (!gsize--)
  241.         return (0);
  242.     *pt++ = *format;
  243.     }
  244.     return (gsize);
  245. }
  246.  
  247. static _secs(struct tm *t)
  248. {
  249.     static char buf[15];
  250.     register time_t s;
  251.     register char *p;
  252.     struct tm tmp;
  253.  
  254.     /* Make a copy, mktime(3) modifies the tm struct. */
  255.     tmp = *t;
  256.     s = mktime(&tmp);
  257.     for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
  258.     *p-- = s % 10 + '0';
  259.     return (_add(++p));
  260. }
  261.  
  262. static _conv(int n, int digits, char pad)
  263. {
  264.     static char buf[10];
  265.     register char *p;
  266.  
  267.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  268.     *p-- = n % 10 + '0';
  269.     while (p > buf && digits-- > 0)
  270.     *p-- = pad;
  271.     return (_add(++p));
  272. }
  273.  
  274. static _add(register char *str)
  275. {
  276.     for (;; ++pt, --gsize) {
  277.     if (!gsize)
  278.         return (0);
  279.     if (!(*pt = *str++))
  280.         return (1);
  281.     }
  282. }
  283.